/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.sesame.domain.Complaint;
import com.sesame.service.UserService;

/**
 * @author voidman
 * 
 */
public class ComplaintController extends SimpleFormController {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {

        Integer userId = (Integer) request.getSession().getAttribute("userId");
        String nickName = (String) request.getSession()
                .getAttribute("nickName");
        Complaint complaint = (Complaint) command;

        Date createTime = new Date();
        complaint.setCreateTime(createTime);
        complaint.setUserId(userId);
        complaint.setNickName(nickName);
        complaint.setState("0"); // state为0表示未处理

        Map model = errors.getModel();
        userService.report(complaint, model);
        model.put("complaint", complaint);

        return new ModelAndView(getSuccessView(), model);

    }
}